package com.datastructure.test.findmedianintwosortedarray;

import java.util.Arrays;

public class FindMedianInTwoSortedArray {

    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4};
        int[] arr2 = {3,4,5,6};
        FindMedianInTwoSortedArray findMedianInTwoSortedArray = new FindMedianInTwoSortedArray();
        int medianinTwoSortedAray = findMedianInTwoSortedArray.findMedianinTwoSortedAray(arr1, arr2);
        System.out.println(medianinTwoSortedAray);
    }

    public int findMedianinTwoSortedAray (int[] arr1, int[] arr2) {
        // write code here
        int halfLength = arr1.length;
        if(halfLength==1){
            if(arr1[0]<arr2[0]){
                return arr1[0];
            } else {
                return arr2[0];
            }
        }
        int length = halfLength *2;
        int[] res = new int[length];
        int i = 0;
        for (int j = 0; j < halfLength; j++,i++) {
            res[j]=arr1[j];
        }
        for (int k = i;k<length;k++){
            res[k]=arr2[k-halfLength];
        }
        Arrays.sort(res);
        int mid = length/2;
        return res[mid-1];
    }
}
